package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class CheckUIText
 */
//@WebServlet(description = "不知道...", urlPatterns = { "/CheckUIText" })
public class CheckUIText extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckUIText() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			Widgets wid = new Widgets();
			ArrayList<UIText> uiTexts = new ArrayList<UIText>();
			String strSQL = "SELECT UITextID,UITextName FROM tms_uitext ORDER BY UITextID";
			String resultQuery = "";
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			
			while (rs.next()) {
				UIText uiText = new UIText();
				uiText.setUiTextID(rs.getInt("UITextID"));
				uiText.setUiTextName(rs.getString("UITextName"));
				uiTexts.add(uiText);
			}
			
			
			resultQuery = new Gson().toJson(uiTexts);
			
			response.setContentType("application/json");
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(resultQuery);	
		} catch (Exception e) {
			Widgets wid = new Widgets();
			wid.keepErrorMes(conn, ps, e, "", true);
		} finally {
			try {
				if (rs!= null) {rs.close();}	
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {}
		}
	}
}

class UIText {
	private int uiTextID;
	private String uiTextName;
	
	public int getUiTextID() {
		return uiTextID;
	}
	public void setUiTextID(int uiTextID) {
		this.uiTextID = uiTextID;
	}
	public String getUiTextName() {
		return uiTextName;
	}
	public void setUiTextName(String uiTextName) {
		this.uiTextName = uiTextName;
	}
}